<?php
error_reporting(0);
    $server = $_POST['namaserver'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $database = $_POST['database'];

    $mysqli = new mysqli($server, $username, $password);
    if ($mysqli->connect_errno) {
        echo "Gagal koneksi ke server MySQL, silahkan cek nama server, username dan password : " . $mysqli->connect_error;
    }

    $query = '
    CREATE DATABASE IF NOT EXISTS '.$database.';
    USE '.$database.';
    CREATE TABLE IF NOT EXISTS asisten (
      idAsisten int(11) NOT NULL AUTO_INCREMENT,
      Nama text NOT NULL,
      NIM text NOT NULL,
      Foto text,
      namaPanggilan varchar(12) NOT NULL,
      PRIMARY KEY (idAsisten)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=14 ;

    CREATE TABLE IF NOT EXISTS config (
      id int(11) NOT NULL,
      username text NOT NULL,
      password text NOT NULL,
      matakuliah text NOT NULL,
      kelas text NOT NULL,
      pesan text NOT NULL,
      pesanDepan text NOT NULL,
      jenisPolling enum("SINGLE","DOUBLE") DEFAULT NULL,
      lihatHasil enum("PUBLIC","PRIVATE") DEFAULT NULL,
      cekIP enum("YA","TIDAK") DEFAULT NULL,
      cekMAC enum("YA","TIDAK") DEFAULT NULL,
      PRIMARY KEY (id)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    CREATE TABLE IF NOT EXISTS vote (
      idVote int(11) NOT NULL AUTO_INCREMENT,
      IPServer text NOT NULL,
      IPVoter text NOT NULL,
      nilai varchar(50) NOT NULL,
      KesanPesan text NOT NULL,
      idAsisten int(11) NOT NULL,
      MACVoter varchar(17) NOT NULL,
      matakuliah text NOT NULL,
      kelas text NOT NULL,
      det varchar(50) DEFAULT NULL,
      PRIMARY KEY (idVote),
      KEY idAsisten (idAsisten)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=7 ;
    
    ALTER TABLE vote
      ADD CONSTRAINT FKasisten FOREIGN KEY (idAsisten) REFERENCES asisten (idAsisten);
      
    INSERT INTO config (id, username, password, matakuliah, kelas, pesan, pesanDepan, jenisPolling, lihatHasil, cekIP, cekMAC) VALUES
    (1, "admin", "admin", "Nama Matakuliah", "kelas", "Jangan lupa setelah selesai poling segera untuk mendownload data poling dan melakukan upload ke server forumasisten di beta.forumasisten.org atau via menu sinkronasi", "Selamat datang di aplikasi voting dan evaluasi asisten, silahkan klik selanjutnya", "DOUBLE", "PRIVATE", "YA", "TIDAK");
    ';
    mysqli_multi_query($mysqli, $query);
    mysqli_close ($mysqli);
    

    $file = fopen("./modules/koneksi.php","w");
    fwrite($file,"<?php
	error_reporting(E_ALL & ~E_NOTICE);
	mysql_connect('". $server."','".$username."','".$password."');
	mysql_select_db('".$database."');
    ?>");
    fclose($file);
	echo "Sukses, Silahkan lihat halaman depan <a href='./'>disini</a> ";
	
?>